import { useIntersectionObserver } from  "@vueuse/core";

//定义懒加载插件
export const lazyPlugin = {
    install(app){
        //懒加载指令逻辑
        app.directive('img-lazy',{
            mounted(el,binding){
                //el :指令绑定的那个元素
                //binding :binding.value 指令等于号后面的表达式的值 -->图片url
                const {stop} = useIntersectionObserver(
                    el,
                    ([{isIntersecting}])=>{
                        // console.log(isIntersecting);
                        //如果进入视口区域,则赋值
                        if(isIntersecting){
                            el.src = binding.value
                            stop()  //停止监听
                        }
                    }
                )
            }
        })
    }
}